package com.example.testutil.util.file;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.example.testutil.util.LogUtil;

public class SAXparseXml extends DefaultHandler {
    private List<LanguageBean> mBeans;
    
    private String tagname;
    
    private LanguageBean languageBean;
    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
        mBeans = new ArrayList<>();
    }

    @Override
    public void endDocument() throws SAXException {
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if(qName.equals("language")) {
            languageBean = new LanguageBean();
            languageBean.id = Integer.valueOf(attributes.getValue(0));
        }
        tagname = qName;
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if(qName.equals("language")) {
            mBeans.add(languageBean);
            LogUtil.d("-----------"+languageBean.toString());
            languageBean = null;
           
        }
        tagname = null;
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if(tagname != null) {
            if(tagname.equals("name")) {
                String name = new String(ch, start, length);
                languageBean.name = name;
            } else if(tagname.equals("usage")) {
                String usage = new String(ch, start, length);
                languageBean.usage = usage;
            }
            
        }
    }

}
